import {
	mapState
} from 'vuex';
export default {
	data() {
		return {
			// 左侧菜单，支持触发的按键集合
			menuKeyCode: ['F6', 'F7', 'F8', 'F9', 'F10', 'F11']
		};
	},
	onShow() {
		this.setNavigationBarTitleText();
		// 监听键盘回调
		window.POS_HOTKEY_CALLBACK = (control, code) => {
			// 触发左侧菜单按键回调
			this.menuTriggerKeyCodeCallBack(code);
		};
	},
	methods: {
		/**
		 * 设置页面标题
		 */
		setNavigationBarTitleText() {
			let pages = getCurrentPages();
			let currentPage = pages[pages.length - 1];
			if (currentPage && currentPage.$holder && currentPage.$holder.navigationBarTitleText) {
				let title = currentPage.$holder.navigationBarTitleText;
				if (this.storeInfo) title += '-' + this.storeInfo.store_name;
				if (title != currentPage.$holder.navigationBarTitleText) uni.setNavigationBarTitle({
					title: title
				})
			} else {
				setTimeout(() => {
					this.setNavigationBarTitleText();
				}, 800)
			}
		},
		// 触发左侧菜单按键回调
		menuTriggerKeyCodeCallBack(code) {
			if (this.menuKeyCode.indexOf(code) != -1) {
				let data = null;
				for (let i = 0; i < this.menu.length; i++) {
					let item = this.menu[i];
					if (item.keyCode == code) {
						data = item;
						break;
					}
				}

				if (data) {

					// #ifdef H5
					if (data.path == this.$route.path) return;
					// #endif
					// #ifdef APP-PLUS
					if (data.path == '/' + this.$mp.page.route) return;
					// #endif

					this.$util.redirectTo(data.path, data.query ?? {});
				}
			}
		}
	},
	filters: {
		/**
		 * 金额格式化
		 * @param {Object} money
		 */
		moneyFormat(money) {
			if (isNaN(money)) return money;
			return parseFloat(money).toFixed(2);
		},
		/**
		 * 时间格式化
		 * @param {Object} time 时间戳
		 * @param {Object} format 输出格式
		 */
		timeFormat(time, format = 'y-m-d h:i:s') {
			var date = new Date();
			date.setTime(time * 1000);

			var y = date.getFullYear();
			var m = date.getMonth() + 1;
			var d = date.getDate();
			var h = date.getHours();
			var i = date.getMinutes();
			var s = date.getSeconds();

			format = format.replace('y', y);
			format = format.replace('m', (m < 10 ? '0' + m : m));
			format = format.replace('d', (d < 10 ? '0' + d : d));
			format = format.replace('h', (h < 10 ? '0' + h : h));
			format = format.replace('i', (i < 10 ? '0' + i : i));
			format = format.replace('s', (s < 10 ? '0' + s : s));

			return format;
		},
		/**
		 * 刷新会员信息
		 */
		refreshMemberInfo() {
			if (this.memberInfo) {
				this.$api.sendRequest({
					url: '/cashier/storeapi/member/info',
					data: {
						member_id: this.memberInfo.member_id
					},
					success: res => {
						if (res.code == 0 && res.data) {
							this.$store.commit('setMemberInfo', res.data);
						}
					}
				})
			}
		},
		mobileFormat(mobile) {
			return mobile.substring(0, 4 - 1) + '****' + mobile.substring(6 + 1);
		}
	},
	computed: {
		getRootFontSize() {
			return this.$store.state.rootSize;
		},
		siteInfo() {
			return this.$store.state.siteInfo;
		},
		storeInfo() {
			return this.$store.state.storeInfo;
		},
		memberInfo() {
			let info = this.$store.state.memberInfo;
			if (info) {
				if (this.$store.state.userInfo.is_admin == 0 && info.mobile) {
					// 非管理员，不能查看会员手机号
					if (info.mobile.indexOf('****') == -1) info.originalMobile = info.mobile;
					info.mobile = info.mobile.substring(0, 4 - 1) + '****' + info.mobile.substring(6 + 1);
				}
			}
			return info;
		},
		defaultImg() {
			return this.$store.state.defaultImg;
		},
		addon() {
			return this.$store.state.addon;
		},
		...mapState(['menu', 'userInfo'])
	},
	watch: {
		'storeInfo.store_id': {
			handler(nval, oval) {
				if (oval && typeof this.switchStoreAfter == 'function') {
					this.switchStoreAfter();
					this.setNavigationBarTitleText();
				}
			},
			deep: true
		}
	}
}